package servlet;

import bean.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        HttpSession session=request.getSession();
        String id=request.getParameter("id");
        String name=request.getParameter("name");
        String sex=request.getParameter("sex");
        String pwd=request.getParameter("password");
        String identity=request.getParameter("identity");
        String verifyCode=request.getParameter("verifyCode");
        String Code=(String) request.getSession().getAttribute("Code");
        String loginInfo = "";
        AthleteBean athleteBean=new AthleteBean();
        athleteBean.link();
        athleteBean.loadAthlete();
        if (Integer.parseInt(identity) == 2) {
            // 获取数据库全部admin的信息

            ArrayList<Athlete> athletes = athleteBean.getAthleteArrayList();
            Map<String, String> athleteMap = athleteBean.getAthleteMap();
            if (verifyCode.equals(Code)) {
                Athlete athlete = new Athlete(id,name,sex,pwd);
                session.setAttribute("userID", id);
                session.setAttribute("userName",name);
                session.setAttribute("userSex",sex);
                session.setAttribute("userPassword", pwd);
                athleteBean.insertAthlete(athlete);
                response.sendRedirect("login.jsp");
            } else {
                loginInfo = "验证吗错误!请重新输入！";
                session.setAttribute("loginInfo", loginInfo);
                response.sendRedirect("register.jsp");
            }

        }else if (Integer.parseInt(identity) == 3) {
            // 获取数据库全部的信息
            RefereeBean refereeBean=new RefereeBean();
            refereeBean.link();
            refereeBean.loadReferee();
            ArrayList<Referee> referees = refereeBean.getRefereeArrayList();
            Map<String, String> refereeMap = refereeBean.getRefereeMap();
            if (verifyCode.equals(Code)) {
                session.setAttribute("userID", id);
                session.setAttribute("userName",name);
                session.setAttribute("userSex",sex);
                Referee referee = new Referee(id,name,sex,pwd);
                session.setAttribute("userPassword", pwd);
                refereeBean.insertReferee(referee);
                response.sendRedirect("login.jsp");
            } else {
                loginInfo = "验证吗错误!请重新输入！";
                session.setAttribute("loginInfo", loginInfo);
                response.sendRedirect("register.jsp");
            }
        }else if (Integer.parseInt(identity) == 4) {
            // 获取数据库全部admin的信息
            VolunteerBean volunteerBean = new VolunteerBean();
            volunteerBean.link();
            volunteerBean.loadVolunteer();
            ArrayList<Volunteer> volunteers = volunteerBean.getVolunteerArrayList();
            Map<String, String> volunteerMap = volunteerBean.getVolunteerMap();
            if (verifyCode.equals(Code)) {
                session.setAttribute("userID", id);
                session.setAttribute("userName",name);
                session.setAttribute("userSex",sex);
                Volunteer volunteer = new Volunteer(id,name,sex, pwd);
                session.setAttribute("userPassword", pwd);
                volunteerBean.insertVolunteer(volunteer);
                response.sendRedirect("login.jsp");
            } else {
                loginInfo = "验证吗错误!请重新输入！";
                session.setAttribute("loginInfo", loginInfo);
                response.sendRedirect("register.jsp");
            }
        }else{
            loginInfo = "无此身份!请重新输入！";
            session.setAttribute("loginInfo", loginInfo);
            response.sendRedirect("register.jsp");
        }

    }
}